<template>
  <div>
    <AplidForm
      ref="formRef"
      v-bind="noRecordpart"
      v-model="formModel"
      @submit="onSubmit">
    </AplidForm>
  </div>
</template>

<script setup lang="ts">
import { cloneDeep } from 'lodash-es'
import { noRecordpart } from '@/views/datacenter/basicData/serviceFacility/config'
import { useCommonStore } from '@/stores/common'
import { useOperate } from '@/views/datacenter/hooks/useConfig'
const { getValue,getContent } = useOperate(3)
const { setTempParams } = useCommonStore()
const emit = defineEmits(['submit'])
const formRef = ref(null)

const rowData: any = inject('nowRowData')
// dpServiceFacilityObject
// dpFacilityEquipmentList
// dpOperateRecordList
// dpSenileProductsList
// dpServiceFacilityExtend
// dpServiceFacilityOther
console.log("========+++++", rowData)
let otherMod = rowData.value?.data?.dpServiceFacilityOther
let baseData = rowData.value?.baseData?.dpServiceFacilityOther || {}

let equipmentData = rowData.value?.data?.dpFacilityEquipmentList
let baseEquipmentData = rowData.value?.baseData?.dpFacilityEquipmentList || {}

let productData = rowData.value?.data?.dpSenileProductsList
let baseProductData = rowData.value?.baseData?.dpSenileProductsList || {}

let recordData = rowData.value?.data?.dpOperateRecordList
let baseRecordData = rowData.value?.baseData?.dpOperateRecordList || {}

console.log('我需要', equipmentData, productData, recordData)

const formModel = ref({
  ...cloneDeep(otherMod),
  dpSenileProductsPOList:productData,
  dpFacilityEquipmentPOList:equipmentData,
  dpOperateRecordPOList:recordData
})
console.log('渲染的数据',formModel)
const onSubmit = (form, done) => {
  const { tempNowObj } = storeToRefs(useCommonStore())
  // 处理几个列表数据
  form.dpFacilityEquipmentPOList.forEach(el => {
    el.soleValue = tempNowObj.value.name,
    el.code=2006
  });
  form.dpSenileProductsPOList.forEach(el => {
    el.soleValue = tempNowObj.value.name,
    el.code = 2005
  });
  form.dpOperateRecordPOList.forEach(el=>{
    el.soleValue = tempNowObj.value.name,
    el.code = 2013
  })
  const params = {
    ...form,
    soleValue: tempNowObj.value.name,
    code:2004
  }
 // 处理变更字段
 // 变更字段
 let changeColumnsBase = noRecordpart.column.filter(
    (item) => item.changeField
  )
  // 得到变更后的老人用品，设施，和运营记录
  let changeData = []
  changeColumnsBase.map((item) => {
    const tempBefore = baseData[item.propName || item.prop]
    const tempAfter = params[item.propName || item.prop]
    if (tempBefore != tempAfter) {
      changeData.push({
        label: item.label,
        before: getValue(tempBefore, item),
        after: getValue(tempAfter, item)
      })
    }
  })

  const changeObj = {
    data: changeData,
    dataChangeContentList: [
      // ...equipmentObj,
      // ...productsObj,
      // ...recordObj,
      ...getContent(baseEquipmentData,params.dpFacilityEquipmentPOList,2006, '养老设施-设施设备', {code:undefined},'id'),
      ...getContent(baseProductData,params.dpSenileProductsPOList,2005, '养老设施-老人用品', { code: undefined },'id'),
      ...getContent(baseRecordData,params.dpOperateRecordPOList,2013, '养老设施-运营记录', { code: undefined },'id'),
      {
        changeBefore: {
          ...baseData,
          dpFacilityEquipmentPOList:undefined,
          dpSenileProductsPOList:undefined,
          dpOperateRecordPOList:undefined,
          // dpFacilityEquipmentPOList:baseEquipmentData,
          // dpSenileProductsPOList:baseProductData,
          // dpOperateRecordPOList:baseRecordData,
          code:undefined
        },
        changeAfter: {
          ...params,
          dpFacilityEquipmentPOList:undefined,
          dpSenileProductsPOList:undefined,
          dpOperateRecordPOList:undefined,
          code:undefined
        },
        changeData: {
          ...params,
          code: 2004
        },
        changeType:'养老服务设施其他信息',
        code: 2004
      }
    ],
    
  }
  setTempParams({
    name: params.soleValue,
    nowChange: changeObj
  })
  emit('submit', changeObj)
}

const onSave = () => {
  formRef.value!.onSubmit()
}
defineExpose({
  onSave
})
</script>

<style scoped></style>
